﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace mForum.Core.Infrastructure.Validation
{
    public class ValidationResult
    {
        private IDictionary<string, IList<string>> _validationErrors;

        public ValidationResult()
        {
            _validationErrors = new Dictionary<string, IList<string>>();
        }

        public IDictionary<string, IList<string>> Errors
        {
            get { return _validationErrors; }
            private set { _validationErrors = value; }
        }

        public bool IsValid
        {
            get { return CountErrors() == 0; }
        }

        public int CountErrors()
        {
            int errorCount = 0;

            foreach (var errorMessages in _validationErrors.Values)
            {
                errorCount += errorMessages.Count;
            }

            return errorCount;
        }

        public void AddError(KeyValuePair<string, string> error)
        {
            if (error.Key != null)
            {
                if (_validationErrors.ContainsKey(error.Key))
                {
                    _validationErrors[error.Key].Add(error.Value);
                }
                else
                {
                    IList<string> newErrorList = new List<string>();
                    newErrorList.Add(error.Value);

                    _validationErrors.Add(new KeyValuePair<string, IList<string>>(error.Key, newErrorList));
                }
            }
        }
    }
}
